<template>
  <div class="login-container">
    <div class="login-background">
      <div class="login-content">
        <div class="system-title-card">
          <div class="system-title">
            <h1>学生管理系统</h1>
            <p>高效、便捷的学生信息管理平台</p>
          </div>
        </div>
      </div>
    </div>
    <div class="login-form-container">
      <div class="login-form-wrapper">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">用户登录</h3>
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="请输入用户名" 
              type="text"
              :prefix-icon="User"
              size="large"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              placeholder="请输入密码" 
              type="password"
              :prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button 
              :loading="loading" 
              type="primary" 
              class="login-button" 
              @click.prevent="handleLogin"
            >
              登录系统
            </el-button>
          </el-form-item>
          <div class="login-tips">
            <span>默认账号：admin</span>
            <span>默认密码：admin123</span>
          </div>
        </el-form>
        <div class="footer">© 2023 学生管理系统 - 技术支持</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const loading = ref(false)
const loginForm = reactive({
  username: 'admin',
  password: 'admin123'
})

const loginRules = {
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
}

const loginFormRef = ref(null)

onMounted(() => {
  console.log('登录页面已加载')
  console.log('当前路由参数:', route.query)
  
  // 检查本地存储中是否有token
  const token = localStorage.getItem('student_system_token')
  console.log('本地存储中的token:', token ? '存在' : '不存在')
  
  // 清除可能存在的过期token
  if (token) {
    console.log('清除可能过期的token')
    localStorage.removeItem('student_system_token')
    userStore.resetToken()
  }
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    console.log('开始验证表单')
    await loginFormRef.value.validate()
    
    console.log('表单验证通过，提交登录请求')
    console.log('登录信息:', { username: loginForm.username })
    
    loading.value = true
    await userStore.loginAction({
      username: loginForm.username,
      password: loginForm.password
    })
    
    console.log('登录成功，获取到token')
    
    // 检查是否有重定向参数
    const redirect = route.query.redirect
    if (redirect) {
      console.log('重定向到:', redirect)
      router.push(redirect)
    } else {
      console.log('重定向到首页')
      router.push('/dashboard')
    }
    
    ElMessage({
      message: '登录成功',
      type: 'success'
    })
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage({
      message: error.message || '登录失败，请检查账号密码',
      type: 'error'
    })
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  overflow: hidden;
  
  .login-background {
    flex: 0.65;
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    position: relative;
    overflow: hidden;
    
    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="rgba(255,255,255,0.05)" fill-opacity="1" d="M0,192L48,197.3C96,203,192,213,288,229.3C384,245,480,267,576,250.7C672,235,768,181,864,181.3C960,181,1056,235,1152,234.7C1248,235,1344,181,1392,154.7L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>');
      background-size: cover;
      background-position: bottom;
      opacity: 0.5;
    }
    
    .login-content {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      width: 100%;
      padding: 0 80px;
      
      .system-title-card {
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(10px);
        border-radius: 16px;
        box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37), 0 4px 12px rgba(0, 0, 0, 0.15);
        border: 1px solid rgba(255, 255, 255, 0.18);
        padding: 40px;
        max-width: 650px;
        margin: 0 auto;
        transform: perspective(1000px) rotateX(2deg);
        transition: all 0.5s ease;
        position: relative;
        overflow: hidden;
        animation: float 6s ease-in-out infinite;
        
        &::before {
          content: '';
          position: absolute;
          top: -50%;
          left: -50%;
          width: 200%;
          height: 200%;
          background: linear-gradient(
            to bottom right,
            rgba(255, 255, 255, 0) 0%,
            rgba(255, 255, 255, 0.1) 50%,
            rgba(255, 255, 255, 0) 100%
          );
          transform: rotate(45deg);
          animation: shine 6s linear infinite;
        }
        
        .system-title {
          h1 {
            font-size: 56px;
            font-weight: 600;
            margin-bottom: 30px;
            letter-spacing: 3px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            background: linear-gradient(to right, #ffffff, #e0e0e0);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            animation: titleGlow 2s ease-in-out infinite alternate;
          }
          
          p {
            font-size: 22px;
            opacity: 0.9;
            max-width: 600px;
            margin: 0 auto;
            line-height: 1.6;
            text-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
          }
        }
      }
    }
  }
  
  .login-form-container {
    flex: 0.35;
    min-width: 450px;
    max-width: 550px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 10;
    
    .login-form-wrapper {
      width: 100%;
      max-width: 380px;
      padding: 0 40px;
    }
  }
  
  .login-form {
    width: 100%;
    
    .title {
      text-align: center;
      margin: 0 0 50px 0;
      color: #333;
      font-size: 32px;
      font-weight: 600;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -15px;
        left: 50%;
        transform: translateX(-50%);
        width: 50px;
        height: 3px;
        background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
        border-radius: 3px;
      }
    }
    
    .login-button {
      width: 100%;
      height: 50px;
      font-size: 16px;
      font-weight: 500;
      letter-spacing: 1px;
      margin-top: 10px;
      background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
      border: none;
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
      }
    }
    
    .login-tips {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      color: #999;
      font-size: 14px;
    }
  }
  
  .footer {
    margin-top: 50px;
    text-align: center;
    color: #999;
    font-size: 14px;
  }
}

@keyframes float {
  0% {
    transform: perspective(1000px) rotateX(2deg) translateY(0px);
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37), 0 4px 12px rgba(0, 0, 0, 0.15);
  }
  50% {
    transform: perspective(1000px) rotateX(2deg) translateY(-15px);
    box-shadow: 0 15px 40px rgba(31, 38, 135, 0.4), 0 10px 20px rgba(0, 0, 0, 0.2);
  }
  100% {
    transform: perspective(1000px) rotateX(2deg) translateY(0px);
    box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37), 0 4px 12px rgba(0, 0, 0, 0.15);
  }
}

@keyframes shine {
  0% {
    left: -100%;
    top: -100%;
  }
  100% {
    left: 100%;
    top: 100%;
  }
}

@keyframes titleGlow {
  from {
    opacity: 0.9;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  }
  to {
    opacity: 1;
    text-shadow: 0 2px 20px rgba(255, 255, 255, 0.3);
  }
}

@media screen and (max-width: 1200px) {
  .login-container {
    .login-background {
      .login-content {
        .system-title-card {
          padding: 30px;
          max-width: 550px;
          
          .system-title {
            h1 {
              font-size: 48px;
            }
            p {
              font-size: 20px;
            }
          }
        }
      }
    }
  }
}

@media screen and (max-width: 992px) {
  .login-container {
    flex-direction: column;
    
    .login-background {
      height: 35%;
      flex: none;
      
      .login-content {
        padding: 0 30px;
        
        .system-title-card {
          padding: 20px;
          max-width: 450px;
          transform: perspective(1000px) rotateX(0deg);
          animation: floatMobile 4s ease-in-out infinite;
          
          .system-title {
            h1 {
              font-size: 32px;
              margin-bottom: 15px;
            }
            
            p {
              font-size: 16px;
            }
          }
        }
      }
    }
    
    .login-form-container {
      width: 100%;
      min-width: auto;
      max-width: none;
      height: 65%;
      flex: none;
      box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.1);
      
      .login-form-wrapper {
        padding: 30px 20px;
      }
    }
  }
}

@media screen and (max-width: 576px) {
  .login-container {
    .login-background {
      .login-content {
        padding: 0 20px;
        
        .system-title-card {
          padding: 15px;
          max-width: 320px;
          
          .system-title {
            h1 {
              font-size: 28px;
              margin-bottom: 10px;
            }
            
            p {
              font-size: 14px;
            }
          }
        }
      }
    }
  }
}

@keyframes floatMobile {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
  100% {
    transform: translateY(0px);
  }
}
</style> 